package com.manager.commons.basemanage.struts.action;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.manager.BaseDao;
import com.manager.commons.BeanFactory;
import com.manager.commons.basemanage.commons.Constants;
import com.manager.commons.basemanage.commons.SelectStaticData;
import com.manager.commons.basemanage.entity.BaseData;
import com.manager.commons.basemanage.entity.BaseDataList;
import com.manager.commons.basemanage.service.BaseDataService;
import com.manager.commons.basemanage.service.BaseManageService;
import com.manager.commons.basemanage.struts.form.BaseDataForm;
import com.manager.commons.exception.BaseException;
import com.manager.commons.page.PageBean;

/**
 * 
 * @author Administrator
 *
 */
public class BaseDataAction extends DispatchAction {
	/**
	 * Service层实例
	 */
	private BaseManageService baseManageService;
	private BaseDataService baseDataService;
	
	/**
	 * 日志
	 */
	private Logger log = Logger.getLogger(this.getClass());

	/**
	 * 实体对象
	 */
	private BaseData baseData;

	// 增删改查
	/**
	 * 获取BaseData列表
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws InvocationTargetException
	 * @throws IllegalAccessException
	 * @throws Exception
	 */
	public ActionForward listBaseData(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws BaseException, IllegalAccessException,
			InvocationTargetException {
		    BaseDataForm baseDataForm = (BaseDataForm)form;
			// 统计总记录数的sql语句
			int resultSize = 0;
			// 初始化pageBean供分页所用
			PageBean pageBean = null;
			// 取得当前页list数据
			List baseDataList = null;
			resultSize = baseManageService.getAllCountBaseData(baseDataForm);
			pageBean = PageBean.getPageBean("baseData", resultSize, request);
			baseDataList = baseManageService.searchBaseData(baseDataForm,pageBean);
			request.setAttribute(Constants.BASEDATALL, baseDataList);
			log.debug("listBaseData" + "结束");

			return mapping.findForward(Constants.SUCCESS);
	}
    
	
	/**
	 * 显示BaseData信息
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws InvocationTargetException
	 * @throws IllegalAccessException
	 */
	public ActionForward showBaseData(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws IllegalAccessException, InvocationTargetException {

		baseData = this.getBaseData(form);
		baseData = (BaseData) this.baseManageService.getObject(baseData, baseData.getId());

		request.setAttribute(Constants.BASEDATA, baseData);

		return mapping.findForward(Constants.SUCCESS);
	}
	
	/**
	 * 查询基础数据类型
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws BaseException
	 * @throws IllegalAccessException
	 * @throws InvocationTargetException
	 */
	public ActionForward searchBaseData(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws BaseException, IllegalAccessException,
			InvocationTargetException {
		baseData = this.getBaseData(form);

		List<BaseData> baseDataList = (List) this.baseDataService.getBaseDataDao().searchBaseData(baseData);
		request.setAttribute(Constants.BASEDATALL, baseDataList);

		return mapping.findForward(Constants.SUCCESS);
	}

	/**
	 * 跳转到新增BaseData页面
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward toAddBaseData(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		return mapping.findForward(Constants.SUCCESS);
	}

	/**
	 * 新增BaseData
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws IllegalAccessException
	 * @throws InvocationTargetException
	 */
	public ActionForward addBaseData(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws IllegalAccessException, InvocationTargetException {
		
		baseData = this.getBaseData(form);
		
		this.baseManageService.insertObject(baseData);
		
		return mapping.findForward(Constants.SUCCESS);
	}

	/**
	 * 跳转到修改BaseData页面
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws InvocationTargetException
	 * @throws IllegalAccessException
	 */
	public ActionForward toUpdateBaseData(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws IllegalAccessException, InvocationTargetException {
		Long id = Long.valueOf(request.getParameter("id"));
		baseData = new BaseData();
		baseData = (BaseData) this.baseManageService.getObject(baseData,id);
		request.setAttribute(Constants.BASEDATA, baseData);
		return mapping.findForward(Constants.SUCCESS);
	}

	/**
	 * 修改BaseData
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws IllegalAccessException
	 * @throws InvocationTargetException
	 */
	public ActionForward updateBaseData(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws IllegalAccessException, InvocationTargetException {
		baseData = this.getBaseData(form);
		this.baseManageService.updateObject(baseData);
		return mapping.findForward(Constants.SUCCESS);
	}

	/**
	 * 删除BaseData
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws IllegalAccessException
	 * @throws InvocationTargetException
	 */
	public ActionForward delBaseData(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws IllegalAccessException, InvocationTargetException {
		baseData = this.getBaseData(form);
		List<BaseDataList> baseDataListList = (List) baseDataService.getBaseDataListList(baseData.getId());
		this.baseManageService.deleteObject(baseData);
		this.baseManageService.deleteObjectList((List)baseDataListList);
		return mapping.findForward(Constants.SUCCESS);
	}
	
	public ActionForward loadBaseData(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws IllegalAccessException, InvocationTargetException {
		
		BaseDao baseDao= (BaseDao) BeanFactory.getInstance().getBean("baseDao");
		BaseData bd=new BaseData();
		SelectStaticData selData=new SelectStaticData();
		selData.clearMap();
		BaseDataList bdList=new BaseDataList();
		List<BaseDataList> baseDataList=(List)baseDao.queryAll(bdList);
		List<BaseData> baseData=(List)baseDao.queryAll(bd);
		for(BaseData o:baseData){
			String p_id=o.getId().toString();
			List orderValue=new ArrayList();
			for(BaseDataList bo:baseDataList){
				String t_id=bo.getTId().toString();
				if(bo.getTId().toString().equals(p_id)){
					orderValue.add(bo);
				}
			}
			if(orderValue.size()>0){
				selData.put(o.getTypeName(), orderValue);
			}
		}
		
		return mapping.findForward(Constants.SUCCESS);
	}


	// get--set
	/**
	 * 获取BaseData实体
	 * 
	 * @param ActionForm
	 * @throws InvocationTargetException
	 * @throws IllegalAccessException
	 * @return the BaseData
	 */
	public BaseData getBaseData(ActionForm form) throws IllegalAccessException,
			InvocationTargetException {
		BaseDataForm baseDataForm = (BaseDataForm) form;
		baseData = new BaseData();
		BeanUtils.copyProperties(baseData, baseDataForm);
		return baseData;
	}

	/**
	 * @param baseData
	 *            the baseData to set
	 */
	public void setBaseData(BaseData baseData) {
		this.baseData = baseData;
	}

	public BaseManageService getBaseManageService() {
		return baseManageService;
	}

	public void setBaseManageService(BaseManageService baseManageService) {
		this.baseManageService = baseManageService;
	}

	public BaseDataService getBaseDataService() {
		return baseDataService;
	}

	public void setBaseDataService(BaseDataService baseDataService) {
		this.baseDataService = baseDataService;
	}
}